//
//  User.swift
//  LongTermInsurance
//
//  Created by zolobdz on 2018/11/7.
//  Copyright © 2018年 HeChao. All rights reserved.
//

import UIKit
import ObjectMapper
class User: Mappable {
    
    
    var userID = ""
    var name = ""
    var sex = ""
    var personNum = ""
    var perCardNum = ""
    // {"NAME":"姓名","Sex":"性别","UserId":"身份证","PersonNum":"个人编号","PerCardNum":"社保卡号"}
    
    required init?(map: Map) {
        
    }
    
    func mapping(map: Map) {
        userID <- map["UserId"]
        name <- map["NAME"]
        sex <- map["Sex"]
        personNum <- map["PersonNum"]
        perCardNum <- map["PerCardNum"]
    }
    
    func birthday() -> String {
        var birth = self.birthdayStrFromIdentityCard(numberStr: userID)
        if birth.count == 8 {
            birth = birth.prefix(3) + "." + ((birth as NSString).substring(with: NSMakeRange(4, 2)) as String) + "." + birth.suffix(2)
        }
        return birth
    }
    
    func birthdayStrFromIdentityCard(numberStr: String) -> (String) {
        guard numberStr.count > 0 else {
            return ""
        }
        var year:String = ""
        var month:String
        var day:String
        //截取前14位
        //        let fontNumber = (numberStr as NSString).substringWithRange(NSMakeRange(0, 14))
        //判断是18位身份证还是15位身份证
        if (numberStr as NSString).length == 18 {
            year = (numberStr as NSString).substring(with: NSMakeRange(6, 4))
            month = (numberStr as NSString).substring(with: NSMakeRange(10, 2))
            day = (numberStr as NSString).substring(with: NSMakeRange(12, 2))
            let result = "\(year).\(month).\(day)"
            print(result)
            return result
        }else{
            year = (numberStr as NSString).substring(with: NSMakeRange(6, 2))
            month = (numberStr as NSString).substring(with: NSMakeRange(8, 2))
            day = (numberStr as NSString).substring(with: NSMakeRange(10, 2))
            let result = "19\(year).\(month).\(day)"
            print(result)
            return result
        }
    }
}
